<template>
  <div class="activities">
    <h2>活动列表</h2>

    <!-- 添加查询表单 -->
    <el-form :inline="true" class="search-form">
      <el-form-item label="活动标题">
        <el-input v-model="searchForm.title" placeholder="请输入活动标题" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态">
          <el-option label="全部" value="" />
          <el-option label="进行中" value="进行中" />
          <el-option label="待定" value="待定" />
          <el-option label="已完成" value="已完成" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="searchForm.timeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="filteredActivities">
      <el-table-column prop="activityId" label="活动编号" width="100" />
      <el-table-column prop="title" label="活动标题" />
      <el-table-column prop="content" label="活动内容" />
      <el-table-column prop="maxParticipants" label="参加人数" width="100" />
      <el-table-column prop="startTime" label="开始时间" width="180" />
      <el-table-column prop="endTime" label="结束时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button 
            v-if="canSignup(scope.row)"
            type="primary" 
            @click="handleSignup(scope.row)"
          >
            报名
          </el-button>
          <el-button 
            v-if="canCheckIn(scope.row)"
            type="success" 
            @click="handleCheckIn(scope.row)"
          >
            签到
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getActivities, signupActivity, checkInActivity } from '@/api/activity'

export default {
  data() {
    return {
      activities: [],
      searchForm: {
        title: '',
        status: '',
        timeRange: []
      }
    }
  },
  created() {
    this.fetchActivities()
  },
  computed: {
    filteredActivities() {
      return this.activities.filter(activity => {
        const matchTitle = !this.searchForm.title || 
          activity.title.includes(this.searchForm.title)
        const matchStatus = !this.searchForm.status || 
          activity.status === this.searchForm.status
        let matchTime = true
        if (this.searchForm.timeRange && this.searchForm.timeRange.length === 2) {
          const startDate = this.searchForm.timeRange[0]
          const endDate = this.searchForm.timeRange[1]
          const activityDate = new Date(activity.startTime)
          matchTime = activityDate >= startDate && activityDate <= endDate
        }
        return matchTitle && matchStatus && matchTime
      })
    }
  },
  methods: {
    async fetchActivities() {
      try {
        const data = await getActivities(this.searchForm)
        this.activities = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    getStatusType(status) {
      const types = {
        '进行中': 'success',
        '待定': 'warning',
        '已完成': 'info'
      }
      return types[status] || 'info'
    },
    canSignup(activity) {
      const now = new Date()
      const signupStart = new Date(activity.signupStart)
      const signupEnd = new Date(activity.signupEnd)
      return now >= signupStart && now <= signupEnd
    },
    canCheckIn(activity) {
      const now = new Date()
      const startTime = new Date(activity.startTime)
      const endTime = new Date(activity.endTime)
      return now >= startTime && now <= endTime
    },
    handleSearch() {
      // 查询逻辑已通过计算属性实现
    },
    resetSearch() {
      this.searchForm = {
        title: '',
        status: '',
        timeRange: []
      }
    },
    async handleSignup(activity) {
      try {
        await signupActivity(activity.activityId)
        this.$message.success('报名成功')
        this.fetchActivities()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleCheckIn(activity) {
      try {
        await checkInActivity(activity.activityId)
        this.$message.success('签到成功')
        this.fetchActivities()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}
</style> 